package com.myl.service;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.myl.bean.Student;
import com.myl.mapper.StudentMapper;
import com.myl.mapper.UseMapper;

public class StudentServiceImp implements StudentService{

	@Override
	public Page selectByPage(Integer currentPage, Integer PageSize) {
		 InputStream inputStream = null;
		SqlSession sqlSession = null;
		Page page = null;

		try {
			 inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			 sqlSession = sessionFactory.openSession(true);
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			page = PageHelper.startPage(currentPage,PageSize);
			mapper.selectAll();

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(sqlSession!=null){
				sqlSession.close();
			}
			if(inputStream!=null){
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
        return page;
	}

	@Override
	public void addStu(Student stu) {
		
		 InputStream inputStream = null;
			SqlSession sqlSession = null;
			

			try {
				 inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");
				SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
				 sqlSession = sessionFactory.openSession(true);
				StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
				
				mapper.addStu(stu);

			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				if(sqlSession!=null){
					sqlSession.close();
				}
				if(inputStream!=null){
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			} 
	}

	@Override
	public void updateStu(Student stu) {
		
		
		InputStream inputStream = null;
		SqlSession sqlSession = null;
		Page page = null;

		try {
			 inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			 sqlSession = sessionFactory.openSession(true);
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			
			mapper.updateStu(stu);

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(sqlSession!=null){
				sqlSession.close();
			}
			if(inputStream!=null){
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} 
		
	}

	@Override
	public void deleteStu(String number) {
		
		
		InputStream inputStream = null;
		SqlSession sqlSession = null;
		Page page = null;

		try {
			 inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			 sqlSession = sessionFactory.openSession(true);
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			
			mapper.deleteStu(number);

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(sqlSession!=null){
				sqlSession.close();
			}
			if(inputStream!=null){
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} 
		
	}
	
	
	
	

}
